import 'package:flutter_test/flutter_test.dart';

void main() {
  ///这种解法，超过时间限制了
  bool isPalindrome(String s) {
    if (s.isEmpty) {
      return true;
    }
    RegExp exp = RegExp(r"[a-zA-Z0-9]", caseSensitive: true);

    Iterable<String> newS =
        s.toLowerCase().split("").where((char) => exp.hasMatch(char));
    print(newS);

    if (newS.isEmpty) {
      return true;
    }

    int start = 0;
    int end = newS.length - 1;
    while (start != end) {
      if (newS.elementAt(start) != newS.elementAt(end)) {
        return false;
      }
      start++;

      if (start == end) {
        break;
      }

      end--;
    }

    return true;
  }

  ///解法2 遍历一次,首尾双指针
  bool isPalindrome2(String s) {
    if(s.isEmpty){
      return true;
    }

    int start = 0;
    int end = s.length - 1;

    RegExp exp = RegExp(r"[a-zA-Z0-9]", caseSensitive: true);

    while (start < end) {
      if (!exp.hasMatch(s[start])) {
        start++;
        continue;
      }

      if (!exp.hasMatch(s[end])) {
        end--;
        continue;
      }

      if (s[start].toLowerCase() != s[end].toLowerCase()) {
        return false;
      }

      start++;
      end--;
    }

    return true;
  }

  test("dart", () {
    print(isPalindrome2("race a car"));
  });
}
